var network = require("../../utils/request.js")
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    photo1:'',
    authStatus: 'notRealName',
    reason: '',
    canvasImgUrl:''
  },

  chooseImageTap: function () {
    let _this = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],//下面显示的数组是拍照还是别的
      itemColor: "#009688",//显示文字的颜色
      success: function (res) {
        if (!res.cancel) {//选中判断
          if (res.tapIndex == 0) {
            _this.chooseWxImage('album')
          } else if (res.tapIndex == 1) {
            _this.chooseWxImage('camera')
          }
        }
      }
    })
  },
  // 绘制图片到canvas上
  chooseWxImage: function (type) {
    const ctx = wx.createCanvasContext('myCanvas')//画布
    var that = this;
    wx.chooseImage({//上传图片事件
      count: 1,//数量
      sizeType: ['original', 'compressed'],//original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], //album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        that.setData({
          photo1:res.tempFilePaths[0]
        })
        ctx.drawImage(res.tempFilePaths[0], 100, 20, 100, 100)//成功选定第一张。后面的四个值分别是left，top，width，height，来控制画布上的图片的位置和大小
        that.prodImageOpt();
        ctx.draw()
      }
    })
  },  
  //生成图片
  prodImageOpt: function () {
    var that = this;
    wx.canvasToTempFilePath({
      canvasId: 'myCanvas',
      success: function success(res) {
        that.saveImage(res.tempFilePath);
      }
    });
  },
  saveImage: function (tempFilePaths) {//这里触发图片上传的方法
    var that = this;
    var url = app.globalData.uploadUrl;
    wx.uploadFile({
      url: url,
      filePath: tempFilePaths,
      name: 'Filedata ',
      method: 'post',
      success(res) {
        var tempRes = JSON.parse(res.data);
        if (tempRes.result == "success") {
          that.setData({
            photo1: tempRes.url
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(app.globalData.consumerid);
    
  },
  initDataAuthStatus:function(){
    var that = this;
    var param = { consumerId: app.globalData.consumerid};
    network.requestLoading('POST', app.globalData.requestUrl + 'getCertificationStatusAPI', param, '加载中', function (res) {
      if (res.result == "success") {
        var authStatus = res.data.authStatus;
        var reason = res.data.reason;
        that.setData({
          authStatus: authStatus,
          reason: reason
        })

      } else {
        wx.showToast({ title: res.message, icon: 'none' })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.initDataAuthStatus();
  },
  //提交认证
  formSubmit:function(e){
    var photo1= this.data.photo1;
    var realName = e.detail.value.realName;
    var unit= e.detail.value.unit;
    if(!photo1){wx.showToast({title: '手持工作证明不能为空',icon:'none' }); return;}
    if (!realName) { wx.showToast({ title: '姓名不能为空', icon: 'none' }); return; }
    if (!unit) { wx.showToast({ title: '工作单位不能为空', icon: 'none' }); return; }
    var that = this;
    let consumerId = app.globalData.consumerid;
    var param = {
      consumerId: consumerId,
      photo1: photo1,
      realName: realName,
      unit: unit
    };
    network.requestLoading('POST', app.globalData.requestUrl + 'getCertificationInfoAPI', param, '加载中', function (res) {
       if(res.result == "success"){
         wx.showToast({ title:"提交成功" })
         that.initDataAuthStatus();
       }else{
         wx.showToast({title: res.message,icon:'none'})
       }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})